from fastapi import Request
import requests
import asyncio

from service import BinanceService, UserService
from common.result.Result import Result
from common.enum import BaseEnum
from common.const import BaseConstant
from util.log import Log
from util.token import Token
from util.binance import binanceAPI

async def RunBinanceAPI(req: Request):
    try:
        # token = req.headers.get("token")
        # if Token().verify_token(token) == None:
        #     return Result.error(BaseEnum.TOKEN_INVALID_ERROR)

        # user = await UserService.getUserByToken(token)
        # if user == None:
        #     return Result.error(BaseEnum.USER_NOT_EXISTS_ERROR)

        # if user.username != 'helloyt':
        #     return Result.error(BaseEnum.ADMIN_PERMISSION_ERROR)
        
        cnt = await binanceAPI.run()

        if cnt > 0:
            return Result.error(BaseEnum.BINANCE_API_TASK_RUNNING)
        
        return Result.success()
    except Exception as e:
        Log.error('运行 币安API 异常', e)
        return Result.error(BaseEnum.SERVICE_ERROR)